package com.masterclient.app.businessmodule.settingmodule.index;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.eoner.baselib.widget.navgation.FCNavigationBar;
import com.eoner.common.config.Config;
import com.eoner.baselibrary.utils.DataCleanManagerUtils;
import com.eoner.baselib.utils.sp.SPUtils;
import com.eoner.managerlibrary.system.SystemConfigStorage;
import com.eoner.managerlibrary.router.AroutePath;
import com.eoner.managerlibrary.user.UserDataManager;
import com.eoner.middlelib.router.intercept.FCBaseIntercept;
import com.eoner.middlelib.router.map.FCRouterPath;
import com.masterclient.app.R;
import com.masterclient.app.base.CashCommonDialog;
import com.masterclient.app.base.FCBusinessActivity;
import com.masterclient.app.businessmodule.settingmodule.license.LicenseActivity;
import com.masterclient.app.modules.bridge.BridgeActivity;
import com.masterclient.app.businessmodule.settingmodule.bankcard.BankCardManageActivity;
import com.masterclient.app.businessmodule.settingmodule.auth.NameAuthActivity;
import com.masterclient.app.businessmodule.usermodule.login.index.QuickLoginActivity;
import com.masterclient.app.businessmodule.settingmodule.AccountSafeActivity;
import com.masterclient.app.businessmodule.settingmodule.index.contract.SettingContract;
import com.masterclient.app.businessmodule.settingmodule.index.contract.SettingPresenter;
import java.io.Serializable;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * 作者：sean
 * 创建时间：2017/7/19 15:30
 * 功能说明：
 */
@Route(path = FCRouterPath.SETTING_PATH)
public class SettingActivity extends FCBusinessActivity<SettingPresenter> implements SettingContract.View {


    @BindView(R.id.rl_user_info)
    RelativeLayout rlUserInfo;
    @BindView(R.id.rl_safe)
    RelativeLayout rlSafe;
    @BindView(R.id.rl_help)
    RelativeLayout rlHelp;
    @BindView(R.id.rl_about_us)
    RelativeLayout rlAboutUs;
    @BindView(R.id.rl_clear)
    RelativeLayout rlClear;
    @BindView(R.id.tv_logout)
    TextView tvLogout;
    @BindView(R.id.ll_logout)
    LinearLayout llLogout;
    @BindView(R.id.tv_version)
    TextView tvVersion;

    @BindView(R.id.clear_cache)
    View clear_cache;
    @BindView(R.id.cache_txt)
    TextView cache_txt;
    @BindView(R.id.rl_license)
    RelativeLayout rlLicense;


    @Override
    protected int requireGetLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void requireSetCustomNavigationBar(FCNavigationBar navigationBar) {
        navigationBar.setTitle("设置");
    }

    @Override
    protected void requireInitUIAndData(Bundle savedInstanceState) {

        if(UserDataManager.getInstance().isSystemLogin()){
            llLogout.setVisibility(View.VISIBLE);
        }
//        about_url = getIntent().getStringExtra("about_url");
//        help = getIntent().getStringExtra("help");
        tvVersion.setText("V" + getAppInfo());


        try {
            cache_txt.setText(DataCleanManagerUtils.getTotalCacheSize(this));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnClick({R.id.rl_license,R.id.about_us_privacy, R.id.rl_about_us,R.id.rl_help,R.id.tv_logout,R.id.rl_user_info,R.id.rl_safe,R.id.clear_cache,R.id.name_auth_layout,R.id.bank_card_layout})
    void onClick(View view){
        switch (view.getId()){
            case R.id.about_us_privacy:
                startActivitry(BridgeActivity.class,new String[][]{{"url", SystemConfigStorage.getInstance().getPrivacyRuleUrl()},{"title","隐私权相关政策"}});
                break;
            case R.id.tv_logout:
                mPresenter.logout(SPUtils.getInstance().getString(Config.CUSTOMERID));
                CookieSyncManager.createInstance(this);
                CookieManager.getInstance().removeAllCookie();
                CookieManager.getInstance().removeSessionCookie();
                CookieSyncManager.getInstance().sync();
                CookieSyncManager.getInstance().startSync();
                break;
            case R.id.rl_safe:
                if (!UserDataManager.getInstance().isSystemLogin()) {
                    startActivitry(QuickLoginActivity.class, new String[][]{{"intentSource", "我的"}});
                    return;
                }
                startActivitry(AccountSafeActivity.class,null);
                break;
            case R.id.rl_user_info:
                if (!UserDataManager.getInstance().isSystemLogin()) {
                    startActivitry(QuickLoginActivity.class, new String[][]{{"intentSource", "我的"}});
                    return;
                }
                ARouter.getInstance().build(FCRouterPath.USER_PERSONAL_PATH).navigation(this,new FCBaseIntercept());
                break;
            case R.id.rl_help:
                startActivitry(BridgeActivity.class,new String[][]{{"url",SystemConfigStorage.getInstance().getHelpCenterUrl()},{"title","帮助中心"}});
                break;
            case R.id.rl_about_us:
                ARouter.getInstance().build(AroutePath.ABOUTUS_ACTIVITY).navigation();
                break;
            case R.id.name_auth_layout:
                if (!UserDataManager.getInstance().isSystemLogin()) {
                    startActivitry(QuickLoginActivity.class, new String[][]{{"intentSource", "我的"}});
                    return;
                }
                if(!UserDataManager.getInstance().getCheckIdCardStatus().equals("1")){
                    //display_ids_dialog(0);
                    ARouter.getInstance().build(FCRouterPath.SETTING_AUTH_PATH)
                            .navigation(SettingActivity.this,new FCBaseIntercept());
                    return;
                }
                startActivitry(NameAuthActivity.class, new String[][]{{"url", ""}});
                break;
            case R.id.bank_card_layout:
                if (!UserDataManager.getInstance().isSystemLogin()) {
                    startActivitry(QuickLoginActivity.class, new String[][]{{"intentSource", "我的"}});
                    return;
                }
                if(!UserDataManager.getInstance().getCheckIdCardStatus().equals("1")){
                    display_ids_dialog(0);
                    return;
                }
                ARouter.getInstance().build(FCRouterPath.BANK_LIST).withString("fromPageType","setting").navigation(this);
                break;
            case R.id.clear_cache:
                DataCleanManagerUtils.clearAllCache(SettingActivity.this);
                showToast("清除成功");
                try {
                    cache_txt.setText(DataCleanManagerUtils.getTotalCacheSize(this));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.rl_license:

                if (SystemConfigStorage.getInstance().getLicenses().size()>0) {
                    Intent intent = new Intent(mContext, LicenseActivity.class);
                    intent.putExtra("images",(Serializable)SystemConfigStorage.getInstance().getLicenses());
                    startActivity(intent);
                }

                break;
                default:
        }
    }

    CashCommonDialog cashCommonDialog;
    private void display_ids_dialog(int type) {//0 未实名认证 1 实名认证银行卡不符
        String title = "";
        String confirm = "";
        if(type == 0){
            title = "为保证您的资金账户安全，请您先绑定本人身份证信息再进行绑卡";
            confirm = "去实名";
        }else if(type == 1){
            title = "您并未绑定本人银行卡请重新绑卡";
            confirm = "去绑定";
        }
        cashCommonDialog = new CashCommonDialog(mContext,title,"取消",confirm);
        cashCommonDialog.setOnChooseListerner(new CashCommonDialog.OnChooseListerner() {
            @Override
            public void cancel() {
                cashCommonDialog.dismiss();
            }

            @Override
            public void confirm() {
                cashCommonDialog.dismiss();
                if(type == 0){
                    ARouter.getInstance().build(FCRouterPath.SETTING_AUTH_PATH)
                            .navigation(SettingActivity.this,new FCBaseIntercept());
                }else if(type == 1){
                    startActivitry(BankCardManageActivity.class, new String[][]{{"url", ""}});
                }
            }
        });
        cashCommonDialog.show();
    }

    private String getAppInfo() {
        try {
            String pkName = mContext.getPackageName();
            String versionName = mContext.getPackageManager().getPackageInfo(
                    pkName, 0).versionName;
            int versionCode = mContext.getPackageManager()
                    .getPackageInfo(pkName, 0).versionCode;
            return versionName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    @Override
    public void logoutSuccess() {
        SPUtils.getInstance().put(Config.LAST_CUSTOMERID,SPUtils.getInstance().getString(Config.CUSTOMERID));
        //TODO:清除用户信息
        UserDataManager.getInstance().clearLoginInfo();

        SPUtils.getInstance().remove(Config.TASK_TIME_SHARE_GOODS);
        llLogout.setVisibility(View.GONE);
        mPresenter.mRxManager.mRxBus.post(Config.LOGOUT_SUCCESS);
        ARouter.getInstance().build(AroutePath.MAIN_ACTIVITY).withString("position","0").navigation();
    }
}
